package com.game.init;

import com.game.frame.*;
import com.game.frame.GameFrame.BeginGameFrame;
import com.game.frame.GameFrame.GameFrameMain.GameCore;
import com.game.utils.DataBaseUtil;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class Main {
    // 创建一个类静态变量
    public static int status = 0;
    public static String userName;
    public static GameCore gameCore;

    public static void main(String[] args) {
        setStatus(0);

        // 启动一个加载窗口
        JFrame loadingFrame = createLoadingFrame();
        
        // 在后台线程中初始化数据库
        SwingWorker<Boolean, Void> worker = new SwingWorker<Boolean, Void>() {
            @Override
            protected Boolean doInBackground() throws Exception {
                return DataBaseUtil.initializeDatabase();
            }

            @Override
            protected void done() {
                try {
                    boolean success = get();
                    loadingFrame.dispose(); // 关闭加载窗口
                    
                    if (success) {
                        // 数据库初始化成功，启动登录界面
                        EventQueue.invokeLater(() -> {
                            JOptionPane.showMessageDialog(null,"成功连接数据库...", "提示", JOptionPane.INFORMATION_MESSAGE);
                        });
                    } else {
                        // 数据库初始化失败，显示错误消息
                        JOptionPane.showMessageDialog(null, 
                            "无法连接到数据库，请检查网络连接后重试。", 
                            "连接错误", 
                            JOptionPane.ERROR_MESSAGE);
                        System.exit(1);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    System.exit(1);
                }
            }
        };
        
        worker.execute();

        addStatusChangeListener(new StatusChangeListener() {
            @Override
            public void onStatusChanged(int status) {
                switch (status){
                    case 0:
                        System.out.println("首页");
                        new FirstPageFrame();
                        break;
                    case 1:
                        System.out.println("登录");
                        new LoginFrame();
                        break;
                    case 2:
                        System.out.println("注册");
                        new RegisterFrame();
                        break;
                    case 3:
                        System.out.println("排行榜");
                        new TheChartsFrame();
                        break;
                    case 4:
                        System.out.println("开始游戏");
                        new BeginGameFrame();
                        break;
                    case 5:
                        System.out.println("结束游戏");
                        new PauseFrame();
                        break;
                }
            }
        });
        new FirstPageFrame();
    }

    /**
     * 创建加载窗口
     */
    private static JFrame createLoadingFrame() {
        JFrame frame = new JFrame("加载中");
        frame.setUndecorated(true); // 无边框
        frame.setAlwaysOnTop(true);
        
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(20, 40, 20, 40));
        
        JLabel label = new JLabel("正在连接数据库...");
        label.setHorizontalAlignment(JLabel.CENTER);
        
        JProgressBar progressBar = new JProgressBar();
        progressBar.setIndeterminate(true);
        
        panel.add(label, BorderLayout.CENTER);
        panel.add(progressBar, BorderLayout.SOUTH);
        
        frame.add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        
        return frame;
    }

    
    public static String getUserName() {
        return userName;
    }

    public static void setUserName(String userName) {
        Main.userName = userName;
    }

    private static List<StatusChangeListener> listeners = new ArrayList<>();

    public static int getStatus() {return status;}

    public static void setStatus(int newStatus) {
        if (newStatus != status){
            status = newStatus;
            notifyListeners(newStatus);
        }
    }

    public static void addStatusChangeListener(StatusChangeListener listener) {
        listeners.add(listener);
    }

    private static void notifyListeners(int newStatus) {
        for (StatusChangeListener listener : listeners) {
            listener.onStatusChanged(newStatus);
        }
    }
}